<template>
  <div class="page-container">
    <!--工具栏-->
    <div class="toolbar" style="float:left;padding-top:10px;padding-left:15px;">
      <el-form :inline="true" :model="filters" :size="size">
        <el-row>
          <el-form-item>
            <el-input type="number" v-model="filters.start_min" style="width: 200px;"  placeholder="最低起送价格"></el-input>
          </el-form-item>
          <el-form-item>
            <el-input type="number" v-model="filters.start_max" style="width: 200px;"  placeholder="最高起送价格"></el-input>
          </el-form-item>
          <el-form-item>
            <el-input type="number" v-model="filters.add_min" style="width: 200px;"  placeholder="最低递增价格"></el-input>
          </el-form-item>
          <el-form-item>
            <el-input type="number" v-model="filters.add_max" style="width: 200px;"  placeholder="最高递增价格"></el-input>
          </el-form-item>
          <el-form-item>
            <el-select v-model="filters.type" style="width: 200px;" placeholder="地区">
              <el-option label="江浙沪" value="1"></el-option>
              <el-option label="京津冀" value="2"></el-option>
              <el-option label="其他地区" value="3"></el-option>
              <el-option label="同城配送" value="4"></el-option>
              <el-option label="自提点" value="5"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-select v-model="filters.shopid" style="width: 200px;" placeholder="所属店铺">
              <el-option v-for="item in options" :key="item.id" :label="item.name" :value="item.id"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-date-picker
              v-model="filters.startDate"
              type="date"
              placeholder="创建时间始"
              format="yyyy 年 MM 月 dd 日"
              value-format="yyyy-MM-dd"
              style="width: 200px;" 
            ></el-date-picker>
          </el-form-item>
          <el-form-item>
            <el-date-picker
              v-model="filters.endDate"
              type="date"
              placeholder="创建时间末"
              format="yyyy 年 MM 月 dd 日"
              value-format="yyyy-MM-dd"
             style="width: 200px;" 
            ></el-date-picker>
          </el-form-item>
          <el-row>
            <el-form-item>
              <kt-button
                icon="fa fa-search"
                :label="$t('action.search')"
                type="primary"
                @click="findPage(null)"
              />
            </el-form-item>
            <el-form-item>
              <kt-button
                icon="fa fa-plus"
                :label="$t('action.add')"
                type="primary"
                @click="handleAdd"
              />
            </el-form-item>
            <el-form-item>
              <kt-button
                icon="fa fa-refresh" :label="$t('action.reset')"
                type="primary"
                @click="resetForm('filters')"
              />
            </el-form-item>
          </el-row>
        </el-row>
      </el-form>
    </div>
    <div class="toolbar" style="float:right;padding-top:10px;padding-right:15px;">
      <el-form :inline="true" :size="size">
        <el-form-item>
          <el-button-group>
            <el-tooltip content="刷新" placement="top">
              <el-button icon="fa fa-refresh" @click="findPage(null)"></el-button>
            </el-tooltip>
            <el-tooltip content="列显示" placement="top">
              <el-button icon="fa fa-filter" @click="displayFilterColumnsDialog"></el-button>
            </el-tooltip>
            <el-tooltip content="导出" placement="top">
              <el-button icon="fa fa-file-excel-o" @click="downloadExcel" id="downloadExcel"></el-button>
            </el-tooltip>
          </el-button-group>
        </el-form-item>
      </el-form>
      <!--表格显示列界面-->
      <table-column-filter-dialog
        ref="tableColumnFilterDialog"
        :columns="columns"
        @handleFilterColumns="handleFilterColumns"
      ></table-column-filter-dialog>
    </div>
    <!--表格内容栏-->
    <cy-table
      :height="350"
      ref="CyTable"
      :data="pageResult"
      :columns="filterColumns"
      :showBatchDelete="false"
      @findPage="findPage"
      @handleEdit="handleEdit"
      @handleDelete="handleDelete"
    ></cy-table>
    <!--新增编辑界面-->
    <el-dialog
      :title="operation?'新增':'编辑'"
      width="40%"
      :visible.sync="dialogVisible"
      :close-on-click-modal="false"
    >
      <el-form
        :model="dataForm"
        label-width="80px"
        :rules="dataFormRules"
        ref="dataForm"
        :size="size"
        label-position="right"
      >
        <el-form-item label="起送价格" prop="start_money">
          <el-input type="number" v-model="dataForm.start_money" auto-complete="off" @input="watchMoney"></el-input>
        </el-form-item>
        <el-form-item label="递增价格" prop="add_money">
          <el-input type="number" v-model="dataForm.add_money" auto-complete="off" @input="watchNums"></el-input>
        </el-form-item>
        <el-form-item label="地区" prop="type">
          <el-select v-model="dataForm.type" placeholder="地区" style="width: 100%">
            <el-option label="江浙沪" :value="1"></el-option>
            <el-option label="京津冀" :value="2"></el-option>
            <el-option label="其他地区" :value="3"></el-option>
            <el-option label="同城配送  " :value="4"></el-option>
            <el-option label="自提点" :value="5"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="所属店铺" prop="shop_id">
          <el-select v-model="dataForm.shop_id" placeholder="所属店铺" style="width: 100%">
            <el-option v-for="item in options" :key="item.id" :label="item.name" :value="item.id"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :size="size" @click.native="dialogVisible = false">{{$t('action.cancel')}}</el-button>
        <el-button
          :size="size"
          type="primary"
          @click.native="submitForm"
          :loading="editLoading"
        >{{$t('action.submit')}}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import PopupTreeInput from "@/components/PopupTreeInput";
import CyTable from "@/views/Core/CyTable";
import KtButton from "@/views/Core/KtButton";
import TableColumnFilterDialog from "@/views/Core/TableColumnFilterDialog";
import { format } from "@/utils/datetime";
import { exportExcel } from "@/utils/excel";
export default {
  components: {
    PopupTreeInput,
    CyTable,
    KtButton,
    TableColumnFilterDialog
  },
  data() {
    return {
      size: "small",
      filters: {
        start_min: "",
        start_max: "",
        add_min: "",
        add_max: "",
        type: "",
        shopid: "",
        startDate: "",
        endDate: ""
      },
      columns: [],
      filterColumns: [],
      pageRequest: { pageNum: 1, pageSize: 10 },
      pageResult: {},
      options: {},
      operation: false, // true:新增, false:编辑
      dialogVisible: false, // 新增编辑界面是否显示
      editLoading: false,
      dataFormRules: {
        start_money: [
          { required: true, message: "请输入起送价格", trigger: "blur" }
        ],
        add_money: [
          { required: true, message: "请输入递增价格", trigger: "blur" }
        ],
        type: [{ required: true, message: "请选择地区", trigger: "blur" }],
        shop_id: [{ required: true, message: "请选择商铺", trigger: "blur" }]
      },

      // 新增编辑界面数据
      dataForm: {
        start_money: "",
        add_money: "",
        type: "",
        shop_id: " "
      }
    };
  },
  methods: {
    //监听价格
    watchMoney(){
      var this_ = this;
      if(this_.dataForm.start_money<0){
         this.$message({ message: "起送价格不能为负" , type: "error" });
         this_.dataForm.start_money = ""
         return
      }
    },
    watchNums(){
      var this_ = this;
      if(this_.dataForm.add_money<0){
        this.$message({message:"递增价格不能为负",type:"error"})
      }
      this_.dataForm.add_money = ""
      return
    },
    // 获取分页数据
    findPage: function(data) {
      this.filters.t = "freightTemplate";
      this.$refs.CyTable.findPage(this.filters);
    },

    // 批量删除
    handleDelete: function(data) {
      var ids = "";
      for (var i = 0; i < data.params.length; i++) {
        ids = ids + data.params[i].id + ",";
      }
      data.params.t = "freightTemplate";
      data.params.ids = ids;
      this.utils.request.deleteUserInfo(data.params, data.callback);
    },
    // 显示新增界面
    handleAdd: function() {
      this.dialogVisible = true;
      this.operation = true;
      this.dataForm = {};
    },
    //列表下载
    downloadExcel() {
      this.$confirm("确定下载列表文件?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          let filename = "运费模版";
          this.$refs.CyTable.exportData(
            this.filters,
            this.filterColumns,
            filename
          );
        })
        .catch(() => {});
    },
    // 显示编辑界面
    handleEdit: function(params) {
      console.log(params);
      this.dialogVisible = true;
      this.operation = false;
      this.dataForm = Object.assign({}, params.row);
    },
    // 编辑
    submitForm: function() {
      this.$refs.dataForm.validate(valid => {
        if (valid) {
          this.$confirm("确认提交吗？", "提示", {}).then(() => {
            this.editLoading = true;
            let params = Object.assign({}, this.dataForm);
            params.t = "freightTemplate";
            this.utils.request.editUserInfo(params, this.editInfoBack);
          });
        }
      });
    },

    // 新增修改回调
    editInfoBack: function(data) {
      if (data.code == "0000") {
        this.$message({ message: "操作成功", type: "success" });
        this.dialogVisible = false;
        this.$refs["dataForm"].resetFields();
      } else {
        this.$message({ message: "操作失败, " + data.msg, type: "error" });
      }
      this.findPage({ t: "freightTemplate" });
      this.dialogVisible = false;
      this.operation = false;
      this.editLoading = false;
    },
    // 时间格式化
    dateFormat: function(row, column, cellValue, index) {
      return format(row[column.property]);
    },
    // 处理表格列过滤显示
    displayFilterColumnsDialog: function() {
      this.$refs.tableColumnFilterDialog.setDialogVisible(true);
    },
    // 处理表格列过滤显示
    handleFilterColumns: function(data) {
      this.filterColumns = data.filterColumns;
      this.$refs.tableColumnFilterDialog.setDialogVisible(false);
    },
    // 处理表格列过滤显示
    initColumns: function() {
      this.columns = [
        { prop: "id", label: "编号", minWidth: 50 },
        { prop: "start_money", label: "起送价格", minWidth: 120 },
        { prop: "add_money", label: "递增价格", minWidth: 120 },
        {
          prop: "type",
          label: "地区",
          minWidth: 120,
          formatter: this.showType
        },
         { prop: "shopname", label: "所属店铺", minWidth: 120 },
        { prop: "create_time", label: "创建时间", minWidth: 120 },
        { prop: "creatorName", label: "创建人", minWidth: 120 },
        { prop: "update_time", label: "修改时间", minWidth: 120 },
        { prop: "updatorName", label: "修改人", minWidth: 120 }

        // {prop:"lastUpdateTime", label:"更新时间", minWidth:120, formatter:this.dateFormat}
      ];
      var temp = [];
      $.each(this.columns, function(key, val) {
        temp.push(val);
      });
      this.filterColumns = temp;
    },
    resetForm(formName) {
      this.filters = {
        start_min: "",
        start_max: "",
        add_min: "",
        add_max: "",
        type: "",
        shopid: "",
        startDate: "",
        endDate: ""
      };
      this.findPage({ t: "freightTemplate" });
    },
    // 所属店铺
    queryUserList() {
      var that = this;
      this.utils.request.queryUserList({ t: "shop" }, function(data) {
        that.options = data.data;
      });
    },
    showType(row, column, cellValue, index) {
      if (cellValue == 1) {
        return "江浙沪";
      }
      if (cellValue == 2) {
        return "京津冀";
      }
      if (cellValue == 3) {
        return "其他地区";
      }
      if (cellValue == 4) {
        return "同城配送";
      }
      if (cellValue == 5) {
        return "自提点";
      }
    }
  },

  mounted() {
    this.initColumns();
    this.queryUserList();
  }
};
</script>

<style scoped>
</style>